//
//  DLMineVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMineVC.h"
#import "DLMineTVHeaderView.h"
#import "DLSettingVC.h"

@interface DLMineVC ()

@property (nonatomic, strong) DLMineTVHeaderView *headView;

@end

@implementation DLMineVC


- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setUpNav];
    
    if (kuserManager.isRequestUserInfo) {
        [self getUserTotalAssets];
        [self getUserYesterdayIncome];
        [self getUserTotalIncome];
    }
}
//查询用户累计收益
- (void) getUserTotalIncome {
    
    NSString *postUrl = HTTPGetUserTotalIncome;
    NSDictionary *postDic = @{@"type" : @"2"};
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            
            NSDictionary *resultDic = responseObject[@"result"];
            kuserManager.userInfoModel.totalIncome = [NSString stringWithFormat:@"%@", resultDic[@"money"]];
            kuserManager.userInfoModel.rate = kuserManager.userInfoModel.totalIncome.floatValue / 1000000 * 100;
            //更新数据了 亲。
            [self.headView updateUserInfo];
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            if ([[responseObject objectForKey:@"code"] intValue]==500) {
                //用户未登录
                if ([[responseObject objectForKey:@"message"] isEqualToString:@"用户未登录！"]) {
                    [self mabyTokenInvalid];
                    return;
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
    }];
}
//查询用户昨日收益
- (void) getUserYesterdayIncome {
    
    NSString *postUrl = HTTPGetUserYesterdayIncome;
    NSDictionary *postDic = @{};
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            
            NSDictionary *resultDic = responseObject[@"result"];
            kuserManager.userInfoModel.yesterdayIncome = [NSString stringWithFormat:@"%@", resultDic[@"money"]];
            //更新数据了 亲。
            [self.headView updateUserInfo];
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            if ([[responseObject objectForKey:@"code"] intValue]==500) {
                //用户未登录
                if ([[responseObject objectForKey:@"message"] isEqualToString:@"用户未登录！"]) {
                    [self mabyTokenInvalid];
                    return;
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
    }];
}
//查询用户总资产
- (void) getUserTotalAssets {
    
    NSString *postUrl = HTTPGetUserTotalAssets;
    NSDictionary *postDic = @{};
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        if (kRequestSuccess(responseObject)) {
            
            NSDictionary *resultDic = responseObject[@"result"];
            kuserManager.userInfoModel.assets = [NSString stringWithFormat:@"%@", resultDic[@"assets"]];
            //更新数据了 亲。
            [self.headView updateUserInfo];
        } else {
            kShowRequestMsg(kSafeString(responseObject[@"message"]));
            if ([[responseObject objectForKey:@"code"] intValue]==500) {
                //用户未登录
                if ([[responseObject objectForKey:@"message"] isEqualToString:@"用户未登录！"]) {
                    [self mabyTokenInvalid];
                    return;
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
    }];
}
- (void) mabyTokenInvalid {
    self.tabBarController.selectedIndex = 0;
//    [kuserManager userLogin];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self layoutSubControls];
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (void) layoutSubControls {
    
    self.plainTableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenSafeAreaHeight - kHeightTabBar);
    self.plainTableView.backgroundColor = kDLViewNormalBgColor;
    self.plainTableView.rowHeight = 0.001;
    [self.view addSubview:self.plainTableView];
//    self.plainTableView.scrollEnabled = NO;
    
    self.plainTableView.tableFooterView = [UIView new];
    
    self.headView = [[DLMineTVHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenSafeAreaHeight - kHeightTabBar)];
    self.plainTableView.tableHeaderView = self.headView;
    
    
}


- (void) setUpNav {
    
//    self.hiddenNavigationBackground = YES;
//    self.hiddenNavigationBottomLine = YES;
    
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    
//    UIButton *systemSetBtn = [UIButton buttonWithType:0];
//    systemSetBtn.frame = CGRectMake(20, 35, 25, 25);
//    [systemSetBtn setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
//    systemSetBtn.backgroundColor = [UIColor redColor];
//    [systemSetBtn setTitle:@"设置" forState:UIControlStateNormal];
//    [systemSetBtn addTarget:self action:@selector(systemSetBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:systemSetBtn];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
//    UILabel *titleLabel = [UILabel new];
//    titleLabel.frame = CGRectMake(systemSetBtn.right, systemSetBtn.top, kScreenWidth - 90, systemSetBtn.height);
//    titleLabel.text = @"我的";
//    titleLabel.font = kBoldFont(18);
//    titleLabel.textColor = [UIColor whiteColor];
//    titleLabel.textAlignment = NSTextAlignmentCenter;
//    [self addSubview:titleLabel];
//    self.titleView.title = @"我的";
    
//    UIButton *machineBtn = [UIButton buttonWithType:0];
//    machineBtn.frame = CGRectMake(kScreenWidth - 39 *2 / 3 - 20, 40, 39 *2 / 3, 27 * 2 /3);
//    [machineBtn setImage:[UIImage imageNamed:@"info_robot"] forState:UIControlStateNormal];
//    [machineBtn addTarget:self action:@selector(machineBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:machineBtn];
//    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void) machineBtnClick {
    DLMachineDetailVC *machineDetailVC = [DLMachineDetailVC new];
    [kuserManager.lastVc.navigationController pushViewController:machineDetailVC animated:YES];
}
- (void) systemSetBtnClick {
    
    DLSettingVC *settingVC = [DLSettingVC new];
    [kuserManager.lastVc.navigationController pushViewController:settingVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
